package com.py.credit.controller;

import com.alibaba.fastjson.JSONObject;
import com.py.credit.common.logs.ExceptionLogger;
import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.entity.Account;
import com.py.credit.entity.EvalInfo;
import com.py.credit.entity.SysUser;
import com.py.credit.entity.common.OptionalColumn;
import com.py.credit.entity.enums.ResourceEvalInfoFieldsEnum;
import com.py.credit.service.EvalInfoService;
import com.py.credit.service.ModelBaseService;
import com.py.credit.service.ReportGetService;
import com.py.credit.service.XypjRemoteBaseService;
import com.py.credit.utils.ExcelUtil;
import com.py.credit.utils.FormulaCalculator;
import com.py.credit.utils.MapCache;
import com.py.credit.utils.NumberUtils;
import com.py.framework.core.common.Page;
import com.py.framework.core.helper.HttpServletHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.MessageFormat;
import java.util.*;

@RestController
@RequestMapping(value = "/evalInfo")
public class EvalInfoController {

    private static final int ZERO = 0;

    private static final String ZERO_STR = "0";

    private final static String FRONT_TABLE_ID = "evalInfo";
    @Autowired
    private EvalInfoService evalInfoService;

    @Autowired
    private ModelBaseService modelBaseService;

    @Autowired
    private XypjRemoteBaseService xypjRemoteBaseService;

    @Autowired
    private ReportGetController reportGetController;

    @Autowired
    private ReportGetService reportGetService;

    @Value("${absolutepath}")
    private String absolutepath;

    @Value("${pdfJsPath}")
    private String pdfJsPath;

    @Value("${googlePath}")
    private String googlePath;

    @Value("${pdfPagePath}")

    private String pdfPagePath;

    @Value("${pdfForXypjPagePath}")

    private String pdfForXypjPagePath;

    @Value("${pdfForXypjPagePathYz}")

    private String pdfForXypjPagePathYz;

    @Value("${platformUrl}")
    private String Eurl;

    /**
     * 获取初始化列
     *
     * @return
     */
    @RequestMapping(value = "/getOptionalColums.json")
    public RestServiceResult<List<OptionalColumn>> getOptionalColums() {
        RestServiceResult<List<OptionalColumn>> r = new RestServiceResult<List<OptionalColumn>>();
        try {
            r = new RestServiceResult<List<OptionalColumn>>(ResourceEvalInfoFieldsEnum.getOptionalColumns());
        } catch (Exception e) {
            r.setCode(RestServiceResult.CODE_UNKNOW);
            r.setMsg("系统异常");
        }
        return r;
    }

    /**
     * @api {get} /shareLog/getTableList.json 获取表规则列表
     * @apiName getTableList
     * @apiVersion 0.0.1
     * @apiGroup 表规则管理
     * @apiParam {String} condition 查询条件(可选)
     * @apiParam {int} limit 每页展示条数 (必需)
     * @apiParam {int} page 当前页数 (必需)
     * @apiSuccess {String} code 状态码:1 成功 -1 失败
     * @apiSuccess {String} msg 错误信息
     * @apiSuccess {int} count 结果集数据量
     * @apiSuccess {Object[]} data 结果集
     * @apiSuccessExample {json} 成功返回结果
     * {"code":0,"msg":null,"count":2,"data":[{"rule_package":"1222","rule_desc":"122222","rule_name":"1","rule_method":"123","id":22,"time":"2018-04-23","managerName":"超级管理员"},{"rule_package":"555","rule_desc":"5555","rule_name":"555","rule_method":"555","id":35,"time":"2018-04-26","managerName":"超级管理员"}]}
     */
    @RequestMapping(value = "/getTableList.json")
    public RestServiceResult<List<Map<String, Object>>> getTableList(
            @RequestParam(value = "condition", defaultValue = "") String condition, String shzt, Page page,
            HttpServletRequest request) {

        String token = HttpServletHelper.getUserToken(request);
        RestServiceResult<List<Map<String, Object>>> result = new RestServiceResult<List<Map<String, Object>>>();
        // 获取当前用户信息
        RestServiceResult<SysUser> user = xypjRemoteBaseService.getCurrentUser(token);
        SysUser userInfo = null;
        if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
            userInfo = user.getData();
            if (null == userInfo || userInfo.getId() <= 0) {
                result.setCode(RestServiceResult.CODE_UNKNOW);
                result.setMsg("系统异常，请稍后再试！");
                ExceptionLogger.error("BatchLogController#getTableList", "未获取到当前用户信息！");
                return result;
            }
        } else {
            result.setCode(user.getCode());
            result.setMsg(user.getMsg());
            return result;
        }
        String[] additionalColumns = {"id", "modelId", "sqr", "reportStatus"};
        String[] configColumns = (String[]) MapCache.get(userInfo.getId() + "_" + FRONT_TABLE_ID);
        List<String> showColumns = (configColumns == null || configColumns.length == 0)
                ? ResourceEvalInfoFieldsEnum.getDefaultShowColumnEN() : Arrays.asList(configColumns);
        // condition="[{columnEN:\"batch_id\",value:\"EX-test-1-20180326-1174\",type:\"varchar\",fuzzy:fasle}]";
        RestServiceResult<List<Map<String, Object>>> r = new RestServiceResult<List<Map<String, Object>>>();
        try {
            List<Map<String, Object>> tableList = evalInfoService.getByConditionForPage(showColumns, additionalColumns,
                    condition, page, shzt);
            r = new RestServiceResult<List<Map<String, Object>>>(page.getTotalCount(), tableList);
        } catch (Exception e) {
            r.setCode(RestServiceResult.CODE_UNKNOW);
            r.setMsg("系统异常！");
        }
        return r;
    }

    @RequestMapping(value = "/export")
    public RestServiceResult<List<Map<String, Object>>> exportData(
            @RequestParam(value = "condition", defaultValue = "", required = true) String condition, String shzt,
            Page page, HttpServletResponse response, HttpServletRequest request) {
        String token = HttpServletHelper.getUserToken(request);
        RestServiceResult<List<Map<String, Object>>> result = new RestServiceResult<List<Map<String, Object>>>();
        // 获取当前用户信息
        RestServiceResult<SysUser> user = xypjRemoteBaseService.getCurrentUser(token);
        SysUser userInfo = null;
        if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
            userInfo = user.getData();
            if (null == userInfo || userInfo.getId() <= 0) {
                result.setCode(RestServiceResult.CODE_UNKNOW);
                result.setMsg("系统异常，请稍后再试！");
                ExceptionLogger.error("BatchLogController#getTableList", "未获取到当前用户信息！");
                return result;
            }
        } else {
            result.setCode(user.getCode());
            result.setMsg(user.getMsg());
            return result;
        }
        page.setLimit(1000);
        String[] configColumns = (String[]) MapCache.get(1000 + "_" + FRONT_TABLE_ID);
        List<String> showColumns = (configColumns == null || configColumns.length == 0)
                ? ResourceEvalInfoFieldsEnum.getDefaultShowColumnEN() : Arrays.asList(configColumns);
        List<Map<String, Object>> tableList = evalInfoService.getByConditionForPage(showColumns, null, condition, page,
                shzt);
        try {
            ExcelUtil.downloadExcel(response, tableList, "评价信息", "评价信息", showColumns,
                    ResourceEvalInfoFieldsEnum.getOptionalColumns());
            return result;
        } catch (Exception e) {
            result.setCode(RestServiceResult.CODE_UNKNOW);
            result.setMsg("评价导出异常，请稍后再试！");
            ExceptionLogger.error("评价导出异常！", e.getMessage(), e);
            return result;
        }
    }

    @RequestMapping(value = "/getAdvancedConditionColumns.json")
    public RestServiceResult<List<OptionalColumn>> getAdvancedConditionColumns() {
        RestServiceResult<List<OptionalColumn>> r = new RestServiceResult<List<OptionalColumn>>();
        try {
            r = new RestServiceResult<List<OptionalColumn>>(ResourceEvalInfoFieldsEnum.getAdvancedConditionColumns());
        } catch (Exception e) {
            r.setCode(RestServiceResult.CODE_UNKNOW);
            r.setMsg("系统异常");
        }
        return r;

    }

    /**
     * 手动选择展示列
     *
     * @param columns
     * @return
     */
    @RequestMapping(value = "/addShowColunms.json")
    public RestServiceResult<String> addShowColunms(
            @RequestParam(value = "columns", required = true) String[] columns) {
        RestServiceResult<String> r = new RestServiceResult<String>();
        try {
            MapCache.set(1000 + "_" + FRONT_TABLE_ID, columns);
        } catch (Exception e) {
            r.setCode(RestServiceResult.CODE_UNKNOW);
            r.setMsg("系统异常");
        }
        return r;
    }

    @RequestMapping(value = "/getRelatedCategoryAndIndexValuesByModelId")
    public RestServiceResult<List<Map>> getRelatedCategoryAndIndexValuesByModelId(String modelId,
                                                                                  HttpServletResponse response, HttpServletRequest request) {
        String token = HttpServletHelper.getUserToken(request);
        RestServiceResult<List<Map>> result = new RestServiceResult<List<Map>>();
        // 获取当前用户信息
        RestServiceResult<SysUser> user = xypjRemoteBaseService.getCurrentUser(token);
        SysUser userInfo = null;
        if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
            userInfo = user.getData();
            if (null == userInfo || userInfo.getId() <= 0) {
                result.setCode(RestServiceResult.CODE_UNKNOW);
                result.setMsg("系统异常，请稍后再试！");
                ExceptionLogger.error("BatchLogController#getTableList", "未获取到当前用户信息！");
                return result;
            }
        } else {
            result.setCode(user.getCode());
            result.setMsg(user.getMsg());
            return result;
        }
        try {
            List<Map> list = evalInfoService.getRelatedCategoryAndIndexValuesByModelId(String.valueOf(userInfo.getId()),
                    modelId);
            result.setData(list);
            return result;
        } catch (Exception e) {
            result.setCode(RestServiceResult.CODE_UNKNOW);
            result.setMsg("共享日志导出异常，请稍后再试！");
            ExceptionLogger.error("共享日志导出异常！", e.getMessage(), e);
            return result;
        }
    }

    @RequestMapping(value = "/viewCheck")
    public RestServiceResult<List<Map>> viewCheck(String modelId, String sqr, String evalId,
                                                  HttpServletResponse response, HttpServletRequest request) {
        String token = HttpServletHelper.getUserToken(request);
        RestServiceResult<List<Map>> result = new RestServiceResult<List<Map>>();
        // 获取当前用户信息
        /*
         * RestServiceResult<SysUser> user =
         * xypjRemoteBaseService.getCurrentUser(token); Account account = (Account)
         * request.getSession().getAttribute("ACCOUNT"); if (null == account ||
         * account.getId() <= 0) {
         * result.setCode(RestServiceResult.CODE_UNKNOW);
         * result.setMsg("系统异常，请稍后再试！");
         * ExceptionLogger.error("BatchLogController#getTableList",
         * "未获取到当前用户信息！"); return result; }
         */
        try {
            List<Map> list = evalInfoService.viewCheck(modelId, sqr, evalId);
            result.setData(list);
            return result;
        } catch (Exception e) {
            result.setCode(RestServiceResult.CODE_UNKNOW);
            result.setMsg("评价详情页面异常，请稍后再试！");
            ExceptionLogger.error("评价详情页面异常！", e.getMessage(), e);
            return result;
        }
    }

    @RequestMapping(value = "/check.do")
    public RestServiceResult<List<Map>> check(String indexs, String evalId, String oldStatus, String newStatus,
                                              String type, String shyj, String cardsType, String userId, HttpServletResponse response,
                                              HttpServletRequest request) {
        String[] indexString = indexs.split(",");
        List indexList = Arrays.asList(indexString);
        String token = HttpServletHelper.getUserToken(request);
        RestServiceResult<List<Map>> result = new RestServiceResult<List<Map>>();
        // 获取当前用户信息
        Account account = (Account) request.getSession().getAttribute("ACCOUNT");
        if (null == account || account.getId() <= 0) {
            result.setCode(RestServiceResult.CODE_UNKNOW);
            result.setMsg("系统异常，请稍后再试！");
            ExceptionLogger.error("BatchLogController#getTableList", "未获取到当前用户信息！");
            return result;
        }
        try {
            evalInfoService.check(indexList, String.valueOf(account.getId()), oldStatus, newStatus, type, shyj, evalId,
                    String.valueOf(account.getId()), cardsType, userId, Eurl);
            if ("5".equals(newStatus) || "00".equals(newStatus)) {
                EvalInfo evalInfo = evalInfoService.selectOne(evalId);
                reportGetService.reportExport(evalInfo.getBgbh(), evalId, pdfForXypjPagePath, pdfJsPath, absolutepath,
                        googlePath, request, String.valueOf(account.getId()));
            }
            return result;
        } catch (Exception e) {
            result.setCode(RestServiceResult.CODE_UNKNOW);
            result.setMsg("评价流程审核异常，请稍后再试！错误提示" + e.getMessage());
            ExceptionLogger.error("评价流程审核异常！", e.getMessage(), e);
            return result;
        }
    }

    @RequestMapping(value = "/preScore.do")
    public RestServiceResult<List<Map>> preScore(String modelId, String sqr, String evalId,
                                                 HttpServletResponse response, HttpServletRequest request) {
        String token = HttpServletHelper.getUserToken(request);
        RestServiceResult<List<Map>> result = new RestServiceResult<List<Map>>();
        // 获取当前用户信息
        RestServiceResult<SysUser> user = xypjRemoteBaseService.getCurrentUser(token);
        /*
         * SysUser userInfo = null; if (RestServiceResult.CODE_SUCCESS ==
         * user.getCode()) { userInfo = user.getData(); if (null == userInfo ||
         * userInfo.getId() <= 0) {
         * result.setCode(RestServiceResult.CODE_UNKNOW);
         * result.setMsg("系统异常，请稍后再试！");
         * ExceptionLogger.error("BatchLogController#getTableList",
         * "未获取到当前用户信息！"); return result; } } else {
         * result.setCode(user.getCode()); result.setMsg(user.getMsg()); return
         * result; }
         */
        try {
            evalInfoService.preScore(modelId, sqr, evalId);
            return result;
        } catch (Exception e) {
            result.setCode(RestServiceResult.CODE_UNKNOW);
            result.setMsg(e.getMessage());
            ExceptionLogger.error("计算分数异常！", e.getMessage(), e);
            return result;
        }
    }

    @RequestMapping(value = "/initScoreByIndex")
    public RestServiceResult<List<Map>> initScoreByIndex(String deleteScoreByIndex, String userId, String value,
                                                         HttpServletResponse response, HttpServletRequest request) {
        String token = HttpServletHelper.getUserToken(request);
        RestServiceResult<List<Map>> result = new RestServiceResult<List<Map>>();
        // 获取当前用户信息
        RestServiceResult<SysUser> user = xypjRemoteBaseService.getCurrentUser(token);
        Account account = (Account) request.getSession().getAttribute("ACCOUNT");
        if (null == account || account.getId() <= 0) {
            result.setCode(RestServiceResult.CODE_UNKNOW);
            result.setMsg("系统异常，请稍后再试！");
            ExceptionLogger.error("BatchLogController#getTableList", "未获取到当前用户信息！");
            return result;
        }
        try {
            evalInfoService.initScoreByIndex(deleteScoreByIndex, userId, value);
            return result;
        } catch (Exception e) {
            result.setCode(RestServiceResult.CODE_UNKNOW);
            result.setMsg("根据指标计算分数异常，请稍后再试！");
            ExceptionLogger.error("根据指标计算分数异常！", e.getMessage(), e);
            return result;
        }
    }

    @RequestMapping(value = "/updateModelIndexValue.do")
    public RestServiceResult<List<Map>> updateModelIndexValue(String indexCatModelId, String sqr, String value,
                                                              String evalId, HttpServletResponse response, HttpServletRequest request) {
        String token = HttpServletHelper.getUserToken(request);
        RestServiceResult<List<Map>> result = new RestServiceResult<List<Map>>();
        // 获取当前用户信息
        RestServiceResult<SysUser> user = xypjRemoteBaseService.getCurrentUser(token);
        Account account = (Account) request.getSession().getAttribute("ACCOUNT");
        if (null == account || account.getId() <= 0) {
            result.setCode(RestServiceResult.CODE_UNKNOW);
            result.setMsg("系统异常，请稍后再试！");
            ExceptionLogger.error("BatchLogController#getTableList", "未获取到当前用户信息！");
            return result;
        }
        try {
            evalInfoService.updateModelIndexValue(evalId, indexCatModelId, sqr, value, String.valueOf(account.getId()));
            return result;
        } catch (Exception e) {
            result.setCode(RestServiceResult.CODE_UNKNOW);
            result.setMsg("更新模型指标值异常，请稍后再试！");
            ExceptionLogger.error("更新模型指标值异常！", e.getMessage(), e);
            return result;
        }
    }

    @RequestMapping(value = "/updateModelIndexScore.do")
    public RestServiceResult<List<Map>> updateModelIndexScore(String indexCatModelId, String modelId, String evalId,
                                                              String sqr, double value, HttpServletResponse response, HttpServletRequest request) {
        String token = HttpServletHelper.getUserToken(request);
        RestServiceResult<List<Map>> result = new RestServiceResult<List<Map>>();
        // 获取当前用户信息
        RestServiceResult<SysUser> user = xypjRemoteBaseService.getCurrentUser(token);
        Account account = (Account) request.getSession().getAttribute("ACCOUNT");
        if (null == account || account.getId() <= 0) {
            result.setCode(RestServiceResult.CODE_UNKNOW);
            result.setMsg("系统异常，请稍后再试！");
            ExceptionLogger.error("BatchLogController#getTableList", "未获取到当前用户信息！");
            return result;
        }
        try {
            evalInfoService.updateModelIndexScore(indexCatModelId, modelId, sqr, value, evalId);
            return result;
        } catch (Exception e) {
            result.setCode(RestServiceResult.CODE_UNKNOW);
            result.setMsg("更新模型指标得分异常，请稍后再试！");
            ExceptionLogger.error("更新模型指标得分异常！", e.getMessage(), e);
            return result;
        }
    }

    @RequestMapping(value = "/getTotalScore.do")
    public RestServiceResult<Map> getTotalScore(String sqr, String modelId, String evalId, HttpServletResponse response,
                                                HttpServletRequest request) {
        String token = HttpServletHelper.getUserToken(request);
        RestServiceResult<Map> result = new RestServiceResult<Map>();
        /*
         * // 获取当前用户信息 RestServiceResult<SysUser> user =
         * xypjRemoteBaseService.getCurrentUser(token); SysUser userInfo = null; if
         * (RestServiceResult.CODE_SUCCESS == user.getCode()) { userInfo =
         * user.getData(); if (null == userInfo || userInfo.getId() <= 0) {
         * result.setCode(RestServiceResult.CODE_UNKNOW);
         * result.setMsg("系统异常，请稍后再试！");
         * ExceptionLogger.error("BatchLogController#getTableList",
         * "未获取到当前用户信息！"); return result; } } else {
         * result.setCode(user.getCode()); result.setMsg(user.getMsg()); return
         * result; }
         */
        try {
            /*
             * double score2=evalInfoService.getTotalScoreByType(sqr,modelId,
             * "2"); score2=NumberUtils.getDouble(2, score2); double
             * score3=evalInfoService.getTotalScoreByType(sqr,modelId, "3");
             * score3=NumberUtils.getDouble(2, score3); double
             * score4=evalInfoService.getTotalScoreByType(sqr,modelId, "4");
             * score4=NumberUtils.getDouble(2, score4); double
             * score5=evalInfoService.getTotalScoreByType(sqr,modelId, "5");
             * score5=NumberUtils.getDouble(2, score5); double
             * score=score2+score3+score4+score5;
             */
            Map<String, Object> scoreMap = evalInfoService.getTotalScore(evalId);
            double scoreAdd = evalInfoService.getExtraScore(evalId, "add");
            double scoreSubtraction = evalInfoService.getExtraScore(evalId, "subtraction");
            double score = (Double) (scoreMap.get("score")) + scoreAdd + scoreSubtraction;
            double score1 = (Double) (scoreMap.get("score1"));
            double score2 = (Double) (scoreMap.get("score2"));
            double score3 = (Double) (scoreMap.get("score3"));
            double score4 = (Double) (scoreMap.get("score4"));
            double score5 = (Double) (scoreMap.get("score5"));
            double score6 = (Double) (scoreMap.get("score6"));
            Map m = modelBaseService.selectOne(modelId);
            double rate = (double) m.get("rate");
            String scoreType = (String) m.get("scoreType");
            if (StringUtils.isNotBlank(scoreType) && "1".equals(scoreType) && rate != 0) {
                score = NumberUtils.getDouble(2, rate * score / 100);
                scoreAdd = NumberUtils.getDouble(2, rate * scoreAdd / 100);
                scoreSubtraction = NumberUtils.getDouble(2, rate * scoreSubtraction / 100);
            }
            scoreMap.replace("score", String.valueOf(score));
            scoreMap.replace("score1", NumberUtils.getDouble(2, rate * score1 / 100));
            scoreMap.replace("score2", NumberUtils.getDouble(2, rate * score2 / 100));
            scoreMap.replace("score3", NumberUtils.getDouble(2, rate * score3 / 100));
            scoreMap.replace("score4", NumberUtils.getDouble(2, rate * score4 / 100));
            scoreMap.replace("score5", NumberUtils.getDouble(2, rate * score5 / 100));
            scoreMap.replace("score6", NumberUtils.getDouble(2, rate * score6 / 100));
            scoreMap.put("scoreSubtraction", String.valueOf(scoreSubtraction));
            scoreMap.put("scoreAdd", String.valueOf(scoreAdd));
            List<Map> l = evalInfoService.getScoreRank(score, modelId);
            if (l != null && l.size() > 0) {
                Map rank = l.get(0);
                String scoreRank = String.valueOf(rank.get("name") == null ? "" : rank.get("name"));
                String evalInfo = String.valueOf(rank.get("evalInfo") == null ? "" : rank.get("evalInfo"));
                scoreMap.put("scoreRank", scoreRank);
                scoreMap.put("evalInfo", evalInfo);
            }

            result.setData(scoreMap);
            return result;
        } catch (Exception e) {
            result.setCode(RestServiceResult.CODE_UNKNOW);
            result.setMsg("获取评价评分异常，请稍后再试！");
            ExceptionLogger.error("获取评价评分异常！", e.getMessage(), e);
            return result;
        }
    }

    @RequestMapping(value = "/getProcessList.json")
    public RestServiceResult<List<Map<String, Object>>> getProcessList(String id, HttpServletResponse response,
                                                                       HttpServletRequest request) {
        String token = HttpServletHelper.getUserToken(request);
        RestServiceResult<List<Map<String, Object>>> result = new RestServiceResult<List<Map<String, Object>>>();
        // 获取当前用户信息
        RestServiceResult<SysUser> user = xypjRemoteBaseService.getCurrentUser(token);
        /*
         * SysUser userInfo = null; if (RestServiceResult.CODE_SUCCESS ==
         * user.getCode()) { userInfo = user.getData(); if (null == userInfo ||
         * userInfo.getId() <= 0) {
         * result.setCode(RestServiceResult.CODE_UNKNOW);
         * result.setMsg("系统异常，请稍后再试！");
         * ExceptionLogger.error("BatchLogController#getTableList",
         * "未获取到当前用户信息！"); return result; } } else {
         * result.setCode(user.getCode()); result.setMsg(user.getMsg()); return
         * result; }
         */
        try {
            List<Map<String, Object>> list = evalInfoService.getProcessList(id);
            if (!CollectionUtils.isEmpty(list)) {
                Set<Integer> userIds = new HashSet<Integer>(list.size());
                for (Map<String, Object> m : list) {
                    if (null != m && m.containsKey("shr") && null != m.get("shr")) {
                        userIds.add(Integer.parseInt(String.valueOf(m.get("shr"))));
                    }
                }
                xypjRemoteBaseService.matchUserName(list, result, token, userIds, "shr", "shrName");
            }
            result.setData(list);
            return result;
        } catch (Exception e) {
            result.setCode(RestServiceResult.CODE_UNKNOW);
            result.setMsg("共享日志导出异常，请稍后再试！");
            ExceptionLogger.error("共享日志导出异常！", e.getMessage(), e);
            return result;
        }
    }

    @RequestMapping(value = "/validateFormular.json")
    public RestServiceResult<Integer> validateFormular(String formular, HttpServletResponse response,
                                                       HttpServletRequest request) {
        String token = HttpServletHelper.getUserToken(request);
        RestServiceResult<Integer> result = new RestServiceResult<Integer>();
        // 获取当前用户信息
        RestServiceResult<SysUser> user = xypjRemoteBaseService.getCurrentUser(token);
        SysUser userInfo = null;
        if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
            userInfo = user.getData();
            if (null == userInfo || userInfo.getId() <= 0) {
                result.setCode(RestServiceResult.CODE_UNKNOW);
                result.setMsg("系统异常，请稍后再试！");
                ExceptionLogger.error("BatchLogController#getTableList", "未获取到当前用户信息！");
                return result;
            }
        } else {
            result.setCode(user.getCode());
            result.setMsg(user.getMsg());
            return result;
        }
        try {
            String value = "111";
            if (formular != null && !"".equals(formular)) {
                float turnDataVal = 0;
                if (value.indexOf("%") > 0) {
                    value = value.substring(0, value.indexOf("%"));
                    turnDataVal = Float.valueOf(value) / 100;
                } else {
                    turnDataVal = Float.valueOf(value);
                }
                String cc = MessageFormat.format(formular, String.valueOf(turnDataVal));
                try {
                    String checkResult = FormulaCalculator.getResult(cc);
                    if (checkResult.indexOf("公式格式有误，请检查") >= 0 || formular.indexOf("{0}") < 0) {
                        result.setData(0);
                        return result;
                    }
                } catch (Exception e) {
                }

            } else {
                result.setData(0);
            }
            result.setData(1);
            return result;
        } catch (Exception e) {
            result.setCode(RestServiceResult.CODE_UNKNOW);
            result.setMsg("共享日志导出异常，请稍后再试！");
            ExceptionLogger.error("共享日志导出异常！", e.getMessage(), e);
            return result;
        }
    }
    
    @RequestMapping("/addapplication.json")
    public RestServiceResult<Object> addapplication(String condition, HttpServletResponse response,
            HttpServletRequest request) {
    		JSONObject json = JSONObject.parseObject(condition);
    	
				return null;
    	
    }
}
